package com.richdown.richx.codec.Md5;

/**
 * Base64算法
 *
 * @author Sunluming
 * @version 2008-6-18 11:48:08
 */
public class Base64
{
    /**
     * Base64编码加密
     *
     * @param data 待加密字节数组
     * @return 加密后字符串
     */
    public static String encode(byte[] data)
    {
        if (data == null) return null;
        return new String(org.bouncycastle.util.encoders.Base64.encode(data));
    }

    /**
     * Base64编码解密
     *
     * @param data 待解密字符串
     * @return 解密后字节数组
     * @throws CodecException 异常
     */
    public static byte[] decode(String data) throws CodecException
    {
        if (data == null) return null;
        try
        {
            return org.bouncycastle.util.encoders.Base64.decode(data.getBytes());
        } catch (RuntimeException e)
        {
            throw new CodecException(e.getMessage(), e);
        }
    }

    public static void main(String[] args) throws CodecException
    {
        System.out.println(Base64.encode("a和法国和a".getBytes()));
        System.out.println(new String(Base64.decode("YeWSjOazleWbveWSjGE=")));
    }
}